package mk.company.fin.frame.report.internal;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyVetoException;
import java.util.List;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

import mk.company.fin.AppConstants;
import mk.company.fin.frame.internal.AbstractInternalFrame;
import mk.company.fin.reports.model.KontoStatusReport;
import mk.company.fin.reports.model.SyntheticCardReport;
import mk.company.fin.service.LocaleService;
import mk.company.fin.service.MessageService;
import mk.company.fin.service.ModelService;

import org.springframework.context.ApplicationContext;

import com.toedter.calendar.JDateChooser;

public class SyntheticCardReportFrame extends AbstractInternalFrame {

	
	private static final long serialVersionUID = -444363727119919664L;
	
	/* SERVICES */
	private MessageService messageService;
	private ModelService modelService;
	private LocaleService localeService;

	
	//component which this frame will be drawn
	JComponent component;
	public SyntheticCardReportFrame(JComponent component) {
		this.component=component;
		
	}
	
	@Override
	public void initialize(ApplicationContext context) {
		setContext(context);
		injectServices();
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(new Dimension(AppConstants.MainFrame.WIDTH, AppConstants.MainFrame.HEIGHT));
		// components
		initComponents();
	}

	/**
	 * Inject services.
	 */
	private void injectServices() {
		messageService = getContext().getBean(MessageService.class);
		modelService=getContext().getBean(ModelService.class);
		localeService=getContext().getBean(LocaleService.class);
	}
	
	public void initComponents() {
		// title
		setTitle(messageService.getMessage("report.synthetic.card"));
		// Size
		this.setSize(AppConstants.MainFrame.WIDTH, AppConstants.MainFrame.HEIGHT);
		// Maximize
		try {
			this.setMaximum(true);
		} catch (PropertyVetoException e) {
		} catch (NullPointerException n) {
		}
		// Layout
		this.setLayout(null);
		
		initReportSearchPanel();
		initReportTablePanel();
		//Set the layout of all the panels
		setLayout();
		
		this.setVisible(true);
		component.add(this);
	}
	
	private javax.swing.JPanel reportSearchPanel;
	private javax.swing.JLabel fromDateLabel;
	private JDateChooser fromDateField;
	private javax.swing.JLabel toDateLabel;
	private JDateChooser toDateField;
	private javax.swing.JButton reportActionButton;
	private javax.swing.JTextField syntheticNumberField;
	private javax.swing.JLabel syntheticNumberLabel;

	private void initReportSearchPanel() {
		reportSearchPanel=new JPanel();
		reportSearchPanel.setBorder(BorderFactory.createTitledBorder(messageService.getMessage("report")));
		fromDateLabel=new JLabel(messageService.getMessage("date.from"));
		fromDateField = new JDateChooser();
		fromDateField.setLocale(localeService.getCurrentLocale());
		toDateLabel=new JLabel(messageService.getMessage("date.to"));
		toDateField=new JDateChooser();
		toDateField.setLocale(localeService.getCurrentLocale());
		syntheticNumberLabel=new JLabel(messageService.getMessage("conto.synthetic"));
		syntheticNumberField=new JTextField();
		syntheticNumberField.setColumns(3);
		reportActionButton=new JButton();
		reportActionButton.setText(messageService.getMessage("report"));
		reportActionButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (!syntheticNumberField.getText().equals("")&&syntheticNumberField.getText().length()!=3){
					return;
				}
				((DefaultTableModel)reportTable.getModel()).getDataVector().removeAllElements();
				List<SyntheticCardReport> resultList=modelService.getSyntheticCardReport(fromDateField.getDate(), toDateField.getDate(),syntheticNumberField.getText());
				for (SyntheticCardReport syntheticCardReport:resultList){
					Vector<String> row=new Vector<String>();
					row.add(syntheticCardReport.getWarrantNumber());
					row.add(syntheticCardReport.getWarrantDate());
					row.add(syntheticCardReport.getDebt());
					row.add(syntheticCardReport.getDemand());
					try {
						row.add((Double.parseDouble(syntheticCardReport.getDebt())-Double.parseDouble(syntheticCardReport.getDebt())+""));
					}
					catch (Exception ex){}
					((DefaultTableModel) reportTable.getModel()).addRow(row);
				}
				reportTable.revalidate();
			}
		});
	}
	
	private javax.swing.JPanel reportPanel;
	private javax.swing.JScrollPane reportScrollPane;
	private javax.swing.JTable reportTable;
	@SuppressWarnings("all")
	private void initReportTablePanel(){
		reportPanel=new JPanel();
		reportPanel.setBorder(BorderFactory.createTitledBorder(messageService.getMessage("report")));
		reportScrollPane = new javax.swing.JScrollPane();
		reportTable = new javax.swing.JTable();
		reportTable.setModel(new javax.swing.table.DefaultTableModel(
	            new Object [][] {
	            },
	            new String [] {
	                "Налог број", "Налог датум", "Тековно должи", "Тековно побарува", "Салдо"
	            }
	        ) {
	            Class[] types = new Class [] {
	                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
	            };
	            boolean[] canEdit = new boolean [] {
	                false, false, false, false, false
	            };

	            public Class getColumnClass(int columnIndex) {
	                return types [columnIndex];
	            }

	            public boolean isCellEditable(int rowIndex, int columnIndex) {
	                return canEdit [columnIndex];
	            }
	        });
	        reportScrollPane.setViewportView(reportTable);
	}
	private void setLayout(){
		javax.swing.GroupLayout reportSearchPanelLayout = new javax.swing.GroupLayout(reportSearchPanel);
        reportSearchPanel.setLayout(reportSearchPanelLayout);
        reportSearchPanelLayout.setHorizontalGroup(
            reportSearchPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(reportSearchPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(reportSearchPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(fromDateLabel)
                    .addComponent(syntheticNumberLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(reportSearchPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(reportSearchPanelLayout.createSequentialGroup()
                        .addComponent(syntheticNumberField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(reportActionButton))
                    .addGroup(reportSearchPanelLayout.createSequentialGroup()
                        .addComponent(fromDateField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(toDateLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(toDateField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        reportSearchPanelLayout.setVerticalGroup(
            reportSearchPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(reportSearchPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(reportSearchPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(fromDateLabel)
                    .addComponent(fromDateField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(toDateLabel)
                    .addComponent(toDateField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(reportSearchPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(syntheticNumberField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(syntheticNumberLabel)
                    .addComponent(reportActionButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        javax.swing.GroupLayout reportPanelLayout = new javax.swing.GroupLayout(reportPanel);
        reportPanel.setLayout(reportPanelLayout);
        reportPanelLayout.setHorizontalGroup(
            reportPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(reportPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(reportScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 659, Short.MAX_VALUE)
                .addContainerGap())
        );
        reportPanelLayout.setVerticalGroup(
            reportPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(reportPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(reportScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(23, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(reportSearchPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(reportPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(reportSearchPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(reportPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
	}
	@Override
	public void draw() {
		injectServices();
		initComponents();
	}

}

